<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use fast\Random;
use app\common\library\Auth;

/**
 * 会员管理
 *
 * @icon fa fa-user
 */
class Agent extends Backend
{
    
    /**
     * Agent模型对象
     * @var \app\admin\model\Agent
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Agent;
        $this->view->assign("genderList", $this->model->getGenderList());
        $this->view->assign("typeList", $this->model->getTypeList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->where("type",'1')
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->where("type",'1')
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            foreach ($list as $row) {
                $row->visible(['id','mobile','nickname','avatar','loginip','joinip','type','createtime','image','num_record','num_done','fans']);
                
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    //粉丝管理
    public function fans($ids = null){
        $list = db("yjy_attention a")
            ->join("user b","a.user_id=b.id","LEFT")
            ->field("b.nickname,b.mobile,b.avatar")
            ->field("from_unixtime(a.createtime,'%Y-%m-%d %H:%i:%s') as createtime")
            ->where("a.agent_id",$ids)
            ->select();
        $this->view->assign("fans",$list);
        return $this->view->fetch();
    }



    //添加经纪人账号
    public function add()
    {
        if ($this->request->isPost()) {
            $mobile = $this->request->request("mobile");
            $nickname = $this->request->request("nickname");
            $password = $this->request->request("password");
            $avatar = $this->request->request("avatar");
            $user = db("user");
            if ($user->where("mobile", $mobile)->value("id")) {
                $this->error("手机号码已存在");
            }
            $salt = Random::alnum();
            $auth = new Auth();
            $res = $user->insert([
                'mobile' => $mobile,
                'nickname' => $nickname,
                'salt' => $salt,
                'password' => $auth->getEncryptPassword($password, $salt),
                'avatar'   => $avatar,
                 'status'   => 'normal',
                'type'    => 1
            ]);
            if ($res) {
                $this->success("注册成功");
            } else {
                $this->error("注册失败");
            }
        }
        return $this->view->fetch();
    }










}
